local lazypath = vim.fn.stdpath("data") .. "/lazy/lazy.nvim"
if not vim.loop.fs_stat(lazypath) then
  vim.fn.system({
    "git",
    "clone",
    "--filter=blob:none",
    "https://github.com/folke/lazy.nvim.git",
    "--branch=stable",
    lazypath,
  })
end
vim.opt.rtp:prepend(lazypath)

vim.api.nvim_create_autocmd("User", {
  pattern = "VeryLazy",
  callback = function()
    local function _trigger()
      vim.api.nvim_exec_autocmds("User", { pattern = "IceLoad" })
    end

    if vim.bo.filetype == "alpha" then
      vim.o.laststatus = 0;
      vim.api.nvim_create_autocmd("BufRead", {
        once = true,
        callback = _trigger,
      })
    else
      _trigger()
    end
  end,
})

local function neovim_lua()
  return {
    -- -- 文件树
    {
      "nvim-neo-tree/neo-tree.nvim",
      cmd = "Neotree",
      keys = {
        { "sv", desc = "打开文件树" },
      },
      dependencies = {
        "nvim-lua/plenary.nvim",
        "nvim-tree/nvim-web-devicons",
        "MunifTanjim/nui.nvim",
      },
      init = function()
        if vim.fn.argc() == 1 then
          local stat = vim.loop.fs_stat(vim.fn.argv(0))
          if stat and stat.type == "directory" then
            require("neo-tree")
          end
        end
      end,
      opts = function()
        return require('plugins.neo-tree-config').config()
      end,
    },
    -- 文件管理器
    -- {
    --   'stevearc/oil.nvim',
    --   dependencies = { { "nvim-mini/mini.icons", opts = {} } },
    -- },
    -- LSP Support
    {
      "neovim/nvim-lspconfig",
      -- lsp 补全
      -- dependencies = { 'saghen/blink.cmp' },
      lazy = true,
      event = { "User IceLoad", "BufReadPost", "BufReadPre" , "BufNewFile"},
      dependencies = {
        "williamboman/mason.nvim",
        "williamboman/mason-lspconfig.nvim",
        'kevinhwang91/nvim-ufo',      -- vim 代码折叠
        "kevinhwang91/promise-async", -- nvim-ufo 依赖
        -- 添加 dressing.nvim 浮动窗口依赖
        {
          "stevearc/dressing.nvim",
          opts = {
            input = {
              -- 设置浮动窗口外观
              border = "single",
              win_options = {
                winblend = 0,
              },
            },
          },
        },
      },
      config = function()
        require("plugins.lsp.setup")
        require("plugins.lsp.statusline").setup()
      end
    },

    -- lsp
    -- {
    --   "williamboman/mason.nvim",
    --   event = {"BufReadPost", "BufNewFile", "VimEnter"},
    --   opts = {},
    -- },

    -- flutter
    {
      'nvim-flutter/flutter-tools.nvim',
      ft = { "dart" },
      dependencies = {
        'nvim-lua/plenary.nvim',
        'stevearc/dressing.nvim', -- optional for vim.ui.select
      },
      config = function()
        require("flutter-tools").setup {}
      end
    },
    -- 自动完成
    {
      "hrsh7th/nvim-cmp",
      lazy = true,
      event = "LspAttach",
      dependencies = {
        "hrsh7th/cmp-nvim-lsp",     -- LSP source for nvim-cmp
        "hrsh7th/cmp-buffer",       -- buffer 补全
        "hrsh7th/cmp-path",         -- 路径补全
        "saadparwaiz1/cmp_luasnip", -- snippets 补全源
        "L3MON4D3/LuaSnip",         -- snippets 引擎
        -- "Jezda1337/nvim-html-css",  -- html css
      },
      config = function()
        require("plugins.cmp.setup")
        -- 加载自定义代码片段
        require("plugins.snippets")
      end
    },

    -- {
    --   "saghen/blink.cmp",
    --   version = "1.*",
    --   dependencies = {
    --     'rafamadriz/friendly-snippets',
    --     "tzachar/fuzzy.nvim",
    --   },
    --   event = {"BufReadPost", "BufNewFile"},
    --   opts = {}
    -- },

    --  lsp 清理垃圾站
    {
      "zeioth/garbage-day.nvim",
      lazy = true,
      event = "LspAttach",
      opts = {
        -- your options here
      }
    },
    -- 自动补全括号
    {
      "windwp/nvim-autopairs",
      event = "InsertEnter",
      config = function()
        require("plugins.autopairs").config()
      end,
    },
    -- 当前单词高亮
    {
      "RRethy/vim-illuminate",
      lazy = true,
      event = "User IceLoad",
      config = function()
        require("plugins.vimIlluminate")
      end
    },
    -- gitsigns 插件配置
    {
      'lewis6991/gitsigns.nvim',
      lazy = true,
      event = "User IceLoad",
      config = function()
        require('plugins.gitsigns-config').config()
      end,
    },
    -- 颜色显示
    {
      'brenoprata10/nvim-highlight-colors',
      lazy = true,
      event = "User IceLoad",
      config = function()
        require("plugins.highlightColors")
      end
    },
  }
end


require("lazy").setup({
  {
    "goolord/alpha-nvim",
    opts = function()
      local dashboard = require("alpha.themes.dashboard")
      local logo = [[
                                             
      ████ ██████           █████      ██
     ███████████             █████ 
     █████████ ███████████████████ ███   ███████████
    █████████  ███    █████████████ █████ ██████████████
   █████████ ██████████ █████████ █████ █████ ████ █████
 ███████████ ███    ███ █████████ █████ █████ ████ █████
██████  █████████████████████ ████ █████ █████ ████ ██████

                          ✧*｡٩(ˊᗜˋ*)و✧*｡
      ]]
      dashboard.section.header.val = vim.split(logo, "\n")
      dashboard.section.buttons.val = {
        dashboard.button("f", " " .. " Find file", ":Telescope find_files <CR>"),
        dashboard.button("r", " " .. " Recent files", ":Telescope oldfiles <CR>"),
        dashboard.button("t", " " .. " Find text", ":Telescope live_grep <CR>"),
        dashboard.button("c", " " .. " Scheme change", ":Telescope colorscheme <CR>"),
        dashboard.button("l", " " .. " Lazy", ":Lazy<CR>"),
        dashboard.button("q", " " .. " Quit", ":qa<CR>"),
      }
      dashboard.section.header.opts.hl = "Function"
      dashboard.opts.layout[1].val = 4
      return dashboard
    end,
    config = function(_, dashboard)
      require("alpha").setup(dashboard.opts)
      vim.api.nvim_create_autocmd("User", {
        callback = function()
          local stats = require("lazy").stats()
          local ms = math.floor(stats.startuptime * 100) / 100
          dashboard.section.footer.val = "  Lazy-loaded "
              .. stats.loaded
              .. "/"
              .. stats.count
              .. " plugins in "
              .. ms
              .. "ms"
          pcall(vim.cmd.AlphaRedraw)
        end,
      })
    end,
  },
  -- 模糊搜索
  {
    "nvim-telescope/telescope.nvim",
    keys = {
      { "cs",       mode = "n", ":Telescope<CR>",                           desc = "Telescope" },
      { "P",        mode = "n", ":Telescope oldfiles<CR>",                  desc = "Telescope" },
      { "sl",       mode = "n", ":Telescope current_buffer_fuzzy_find<CR>", desc = "Telescope" },
      { "<space>b", mode = "n", ":Telescope buffers<CR>",                   desc = "Telescope" },
      { "<C-f>",    mode = "n", ":Telescope find_files<CR>",                desc = "Telescope" },
      { "sg",       mode = "n", ":Telescope live_grep<CR>",                 desc = "Telescope" },
      { "<space>y", mode = "n", ":Telescope neoclip<CR>",                   desc = "Telescope" },
    },
    cmd = "Telescope",
    dependencies = {
      'nvim-tree/nvim-web-devicons',
      "nvim-lua/plenary.nvim",
      "AckslD/nvim-neoclip.lua", -- 剪切板历史
      { 'kkharji/sqlite.lua', module = 'sqlite' },
    },
    config = function()
      require('plugins.telescope')
    end,
  },
  -- json文件
  {
    'elzr/vim-json',
    ft = { "json", "jsonc" },
  },
  --
  -- 启动时间
  {
    'dstein64/vim-startuptime',
    cmd = "StartupTime",
  },
  -- bufferline
  -- {
  --   'akinsho/bufferline.nvim',
  --   lazy = true,
  --   -- event = "VeryLazy",
  --   event = "User IceLoad",
  --   dependencies = {
  --     'nvim-tree/nvim-web-devicons'
  --   },
  --   config = function()
  --     require('plugins.bufferline')
  --   end
  -- },
  -- 快速移动
  {
    'ggandor/leap.nvim',
    keys = { "<space><space>", mode = "n" },
    config = function()
      require('plugins.leap')
    end,
  },
  -- 注释
  {
    'numToStr/Comment.nvim',
    keys = {
      { "<S-x>", mode = "n" },
      { "<C-x>", mode = "v" },
    },
    config = function()
      require('plugins.comment')
    end,
  },
  -- markdown 预览
  {
    "iamcco/markdown-preview.nvim",
    build = "cd app && yarn install",
    ft = "markdown"
  },
  {
    'dhruvasagar/vim-table-mode',
    ft = "markdown"
  },
  -- sw 保存
  {
    'lambdalisue/suda.vim',
    keys = { "sw", mode = "n" }
  },
  -- 模糊选中
  {
    'gcmt/wildfire.vim',
    keys = { "n", "<CR>" },
  },
  -- 替换引号
  {
    'machakann/vim-sandwich',
    keys = {
      { "sr", mode = "v" },
      { "sa", mode = "v" },
    }
  },
  -- 多光标
  {
    'mg979/vim-visual-multi',
    keys = {
      { "<C-s>", mode = "n" },
      { "<C-s>", mode = "v" },
    },
  },
  -- 自动对齐
  {
    'godlygeek/tabular',
    event = "User IceLoad",
  },
  -- 浮动终端
  {
    'voldikss/vim-floaterm',
    cmd = "FloatermNew",
    config = function()
      require("plugins.VimFloatterm")
    end
  },
  -- 历史记录
  {
    'mbbill/undotree',
    cmd = "UndotreeToggle",
    config = function()
      require("plugins.undotree")
    end
  },
  -- 切换关键字
  {
    'https://gitee.com/wzy-20041202/antovim',
    cmd = "Antovim",
  },
  -- 数据库可视化
  {
    'kristijanhusak/vim-dadbod-ui',
    dependencies = {
      {
        'tpope/vim-dadbod',
        lazy = true
      },
      {
        'kristijanhusak/vim-dadbod-completion',
        ft = { 'sql', 'mysql', 'plsql', 'mongodb' },
        lazy = true
      },
    },
    cmd = {
      'DBUI',
      'DBUIToggle',
      'DBUIAddConnection',
      'DBUIFindBuffer',
    },
    init = function()
      -- Your DBUI configuration
      vim.g.db_ui_use_nerd_fonts = 1
    end,
  },
  -- neovim
  neovim_lua(),
  {
    'catppuccin/nvim',
    lazy = true,
    name = 'catppuccin',
  },
  -- 文本高亮
  {
    'nvim-treesitter/nvim-treesitter',
    lazy = true,
    event = "User IceLoad",
    dependencies = {
      -- "Bekaboo/dropbar.nvim",
      "windwp/nvim-ts-autotag", -- html 标签功能
    },
    config = function()
      require('plugins.treesitter')
      -- require('plugins.dropbar')
    end
  },
  --- 类似 vscode 的自动缩进
  {
    'vidocqh/auto-indent.nvim',
    event = "InsertEnter",
    opts = {
      indentexpr = function(lnum)
        return require("nvim-treesitter.indent").get_indent(lnum)
      end
    }
  },

})

-- vim lsp
-- vim.lsp.enable 'lua_ls'
-- vim.lsp.enable 'pyright'
